package com.sj.parksdk.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.fragment.app.Fragment;

import com.sj.parksdk.R;
import com.sj.parksdk.setting.Extras;
import com.sj.parksdk.utils.XToastUtils;
import com.xuexiang.xhttp2.logs.HttpLog;
import com.xuexiang.xqrcode.XQRCode;
import com.xuexiang.xqrcode.ui.CaptureActivity;
import com.xuexiang.xui.utils.ResUtils;

public class ActivityQrCode extends CaptureActivity implements View.OnClickListener{
    /**
     * 开始二维码扫描
     *
     * @param fragment
     * @param requestCode 请求码
     * @param theme       主题
     */
    public static void start(Fragment fragment, int requestCode, int theme) {
        mRequestCode = requestCode;
        Intent intent = new Intent(fragment.getContext(), ActivityQrCode.class);
        intent.putExtra(KEY_CAPTURE_THEME, theme);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 开始二维码扫描
     *
     * @param activity
     * @param requestCode 请求码
     * @param theme       主题
     */
    public static void start(Activity activity, int requestCode, int theme) {
        mRequestCode = requestCode;
        Intent intent = new Intent(activity, ActivityQrCode.class);
        intent.putExtra(KEY_CAPTURE_THEME, theme);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 开始二维码扫描
     *
     * @param fragment
     * @param requestCode 请求码
     */
    public static void start(Fragment fragment, int requestCode) {
        mRequestCode = requestCode;
        Intent intent = new Intent(fragment.getContext(), ActivityQrCode.class);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 开始二维码扫描
     *
     * @param activity
     * @param requestCode 请求码
     */
    public static void start(Activity activity, int requestCode) {
        mRequestCode = requestCode;
        Intent intent = new Intent(activity, ActivityQrCode.class);
        activity.startActivityForResult(intent, requestCode);
    }

    private AppCompatImageView mIvFlashLight;
    private TextView mTvFlashLight;
    private boolean mIsOpen;
    private static int mRequestCode;

    //===============================重写UI===================================//

    @Override
    protected int getCaptureLayoutId() {
        return R.layout.parksdk_activity_orcode;
    }

    @Override
    protected void beforeCapture() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        mIvFlashLight = findViewById(R.id.iv_flash_light);
        mTvFlashLight = findViewById(R.id.tv_flashlight);
    }

    @Override
    protected void onCameraInitSuccess() {
        mIsOpen = XQRCode.isFlashLightOpen();
        mTvFlashLight.setText(ResUtils.getString(mIsOpen?R.string.orcode_close_flash:R.string.orcode_open_flash));
        mIvFlashLight.setOnClickListener(this);
    }

    @Override
    protected void onCameraInitFailed() {
        mIvFlashLight.setVisibility(View.GONE);
    }

    private void switchFlashLight() {
        mIsOpen = !mIsOpen;
        try {
            XQRCode.switchFlashLight(mIsOpen);
            mTvFlashLight.setText(ResUtils.getString(mIsOpen?R.string.orcode_close_flash:R.string.orcode_open_flash));
        } catch (RuntimeException e) {
            e.printStackTrace();
           XToastUtils.error("设备不支持闪光灯!");
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.iv_flash_light) {
            switchFlashLight();
        }
    }

    //===============================重写业务处理===================================//

    /**
     * 处理扫描成功（重写扫描成功，增加applink拦截）
     *
     * @param bitmap
     * @param result
     */
    @Override
    protected void handleAnalyzeSuccess(Bitmap bitmap, String result) {
//        if (isAppLink(result)) {
//            openAppLink(this, result);
//        } else if (isWeb(result)) {
//            Utils.goWeb(this, result);
//        } else {
//            Intent resultIntent = new Intent();
//            Bundle bundle = new Bundle();
//            bundle.putInt(XQRCode.RESULT_TYPE, XQRCode.RESULT_SUCCESS);
//            bundle.putString(XQRCode.RESULT_DATA, result);
//            resultIntent.putExtras(bundle);
//            setResult(RESULT_OK, resultIntent);
//        }
//        if(mRequestCode == Extras.REQUEST_CODE_QRCODE_COUPONS){
//            Intent resultIntent = new Intent();
//            Bundle bundle = new Bundle();
//            bundle.putInt(Extras.BASE_TYPE, XQRCode.RESULT_SUCCESS);
//            bundle.putString(Extras.BASE_RESULT, result);
////            resultIntent.putExtra(Extras.BASE_RESULT,result);
//            setResult(RESULT_OK, resultIntent);
//            finish();
//        }else{
            startActivity(new Intent(this,ActivityQrCodeSuccess.class).putExtra(Extras.BASE_RESULT,result));
            HttpLog.e(result);
//            ToastUtils.toast(result);
            finish();
//        }
    }


    /**
     * 格式：https://xuexiangjys.club/xpage/transfer?pageName=xxxxx&....
     * 例子：https://xuexiangjys.club/xpage/transfer?pageName=UserGuide&position=2
     *
     * @param url
     * @return
     */
//    private boolean isAppLink(String url) {
//        Uri uri = Uri.parse(url);
//        return uri != null
//                && APP_LINK_HOST.equals(uri.getHost())
//                && (url.startsWith("http") || url.startsWith("https"))
//                && url.contains("xpage");
//    }
//
//    private boolean isWeb(String url) {
//        return !StringUtils.isEmpty(url)
//                && (url.startsWith("http") || url.startsWith("https"));
//    }
//
//    private void openAppLink(Context context, String url) {
//        try {
//            Intent intent = new Intent(APP_LINK_ACTION);
//            intent.setData(Uri.parse(url));
//            context.startActivity(intent);
//        } catch (Exception e) {
//            XToastUtils.error("您所打开的第三方App未安装！");
//        }
//    }
}
